package com.tianqu.tms.www.bid.controller;

import java.util.List;

import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsController;
import com.tianqu.tms.core.base.TxController;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.core.exception.TransactionException;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.entity.AucManagement;
import com.tianqu.tms.www.bid.viewhelper.BidCarQuotationViewHelper;

public class BidCarQuotationController extends TmsController {
	
	BidCarQuotationViewHelper view = new BidCarQuotationViewHelper();

	private Logger logger = Logger.getLogger(BidCarQuotationController.class);

	@Override
	public void execute(UserContext uc) throws Exception {
		
		logger.debug(this.getClass().getName() + "#execute was called.");
		
		// 查询当前时间场次的所有车辆
		// 获得结果集list 根据不同的车辆状态区分车辆列表
		// 所有查询均走一个车辆列表
		TxController controller = TxController.getTxController();

		// 添加事务提交异常捕获
		try {
			controller.begin();
			AucManagement aucManage = AucManagement.getInstance();
			List<AucManagement> list = aucManage.getAucManagementList();
			
			// 等待寻价、寻价开始、寻价结束（竞价结束，成交，流拍）车辆数量
			inputView(list);

			uc.setAttribute(TmsConstants.VIEWHELP, view);
			controller.commit();
		} catch (Exception e) {
			try {
				controller.rollback();
			} catch (TransactionException e1) {
				throw new TmsException(TmsException.DB_TXCRROR, e1);
			}
			// 系统异常
			if (e instanceof TmsException) {
				throw e;
			}
			throw new TmsException(TmsException.DB_TXCRROR, e);
		}

		logger.debug(this.getClass().getName() + "#execute was completed.");
	}

	// view赋值
	private void inputView(List<AucManagement> list) {

		if (list != null && list.size() > 0) {
			for (int i = 0; i < list.size(); i++) {
				AucManagement bid = list.get(i);

				if (bid.getBidStatus() == 1) {
					
					// 等待竞价
					view.setBidCarQuotationWaitNum(view.getBidCarQuotationWaitNum() + 1);
					
				} else if (bid.getBidStatus() == 2) {
					
					//竞价开始
					view.setBidCarQuotationStartNum(view.getBidCarQuotationStartNum() + 1);
					
				} else if (bid.getBidStatus() == 3
						|| bid.getBidStatus() == 4
						|| bid.getBidStatus() == 5
						|| bid.getBidStatus() == 6) {
					
					// 竞价结束，成交，流拍，回访成交  modify by mufeiyu
					view.setBidCarQuotationFinishNum(view.getBidCarQuotationFinishNum() + 1);
				}
			}
		}
	}
}
